package com.aboverock.module.system.service.impl;

import java.util.UUID;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aboverock.module.system.domain.AppKey;
import com.aboverock.module.system.mapper.AppKeyMapper;
import com.aboverock.module.system.service.AppKeyService;

/**
 * The Class AppKeyServiceImpl.
 *
 * @author Rock Wang
 */
@Service
public class AppKeyServiceImpl extends ServiceImpl<AppKeyMapper, AppKey> implements AppKeyService {

    @Override
    public AppKey getByAppKey(String appKey) {
        QueryWrapper<AppKey> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_key", appKey);
        return this.getOne(queryWrapper);
    }

    @Override
    public boolean save(AppKey entity) {
        String appKey = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        entity.setAppKey(appKey);
        entity.setEnabled(1);
        return super.save(entity);
    }

    @Override
    public boolean updateById(AppKey entity) {
        AppKey old = getById(entity.getId());
        if (old == null) {
            return false;
        }
        entity.setAppKey(old.getAppKey());
        return super.updateById(entity);
    }
}
